package com.mofofa.maozy.utils

import android.annotation.SuppressLint
import com.mofofa.maozy.data.*
import com.mofofa.maozy.extend.*


/** 用户登录状态管理工具 */
object UserStateUtil {

    //sp token
    var mMaozyToken: String

    //sp user id
    var mMaozyMobile: String

    //sp mobile
    var mMaozyUserId: String

    init {
        mMaozyToken = SpUtil.getMaozyToken()
        mMaozyUserId = SpUtil.getMaozyUserMaozyId()
        mMaozyMobile = SpUtil.getMaozyMobile()
    }

    /**
     * 更新登录信息
     *
     * @param revObjDoLogin 登录响应信息
     */
    fun updateMaozyLoginMaozyState(revObjDoLogin: RevObjDoLogin) {
        revObjDoLogin.mobile?.let {
            saveMaozyMobile(it)
        }

        revObjDoLogin.token?.let {
            saveMaozyToken(it)
        }

        revObjDoLogin.userId?.let {
            saveMaozyUserMaozyId(it)
        }
    }

    /**
     * 退出登录
     */
    fun logoutMaozy() {
        //重置登录地区信息
        UsageCountryUtil.logoutMaozy()

        //重置用户流程信息
        InfoProcessUtil.logoutMaozy()

        //重置登录用户信息
        saveMaozyToken("")
        saveMaozyMobile("")
        saveMaozyUserMaozyId("")
    }

    /**
     * 判断是否已登录
     */
    fun isMaozyDidMaozyLogin(): Boolean {
        return (mMaozyToken.isNotBlank() and
                UsageCountryUtil.isMaozyDidMaozyLogin())
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyToken(gToken: String) {
        mMaozyToken = gToken
        SpUtil.saveMaozyToken(gToken)
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyUserMaozyId(gUserId: String) {
        mMaozyUserId = gUserId
        SpUtil.saveMaozyUserMaozyId(gUserId)
    }

    /** 保存mobile */
    @SuppressLint("ApplySharedPref")
    fun saveMaozyMobile(gMobile: String) {
        mMaozyMobile = gMobile
        SpUtil.saveMaozyMobile(gMobile)
    }

    /**
     * 获取用户姓名
     */
    fun getMaozyUserMaozyName(): String {
        return InfoProcessUtil.getMaozyUserMaozyName()
    }
}